var dbConfig = require('../utils/dbConfig')
let fs=require('fs')
// 获取用户
getUser = (req, res) => {
  let userID=req.query.userID
  let sql = "select * from user where userID=?"
  let sqlArr = [userID]
  let callBack = (err, data) => {
    if (err) {
      console.log("连接出错了")
    } else {
      res.send({
        'code':200,
        'data': data[0]
      })
    }
  }
  dbConfig.sqlConnect(sql, sqlArr, callBack)
}

// 获取通知
getNotice=(req,res)=>{
  let type = req.query.type
  let noticeID=req.query.noticeID
  var sql = "select * from notice where type=? or noticeID=?"
  var sqlArr = [type,noticeID]
  var callBack = (err, data) => {
    if (err) {
      console.log("连接出错了")
    } else {
      res.send({
        'code':200,
        'data': data
      })
    }
  }
  dbConfig.sqlConnect(sql, sqlArr, callBack)
}
//提交建议
saveAdvice=(req,res)=>{
  let {type,content,phone,name}=req.body
  let isAnonymous="true"
  if(name==='') isAnonymous="false"
  let files = req.files
	console.log(files)
  let fileList=''
  for(let i in files){
    let file = files[i];
    fs.renameSync('./public/uploads/' + file.filename, './public/uploads/advice/' + file.originalname)
    fileList=fileList+`http://localhost:3000/uploads/advice/${file.originalname};`
  }
  var sql = "insert into advice(type,content,phone,isAnonymous,name,img) value (?,?,?,?,?,?)"
  var sqlArr = [type,content,phone,isAnonymous,name,fileList]
	var callBack = (err, data) => {
    if (err) {
      console.log("连接出错了",err)
    } else {
      res.send({
        'code':200,
        'tip':'提交成功'
      })
    }
  }
  dbConfig.sqlConnect(sql, sqlArr, callBack)

}
module.exports = {
  getUser,
  getNotice,
  saveAdvice
}